//可编辑初始化
function editable(elem){
	addEvent(elem,"click",function(){
		var div=elem;
		div.setAttribute("contenteditable",true);
		div.style.backgroundColor="#fff";
		d_collapse(div);
	});
	addEvent(elem,"blur",function(){
		var div=elem;
		div.setAttribute("contenteditable",false);
		div.style.backgroundColor="transparent";
	});
}
//折叠dom范围,将光标移到结尾
function d_collapse(elem){
	if(document.all){
		var range=document.body.createTextRange();
		range.moveToElementText(elem);
		range.collapse(false);
		range.select();
		elem.focus();
	}else{
		var range=document.createRange();
		range.selectNodeContents(elem);
		range.collapse(false);
		elem.focus();
	}
}
//操作段落
function addP(elem){
	var parent=elem.parentNode;
	var next=parent.nextSibling;
	//创建区域
	var frag=document.createDocumentFragment();
	//创建内容编辑区域
	var div=document.createElement("div");
	div.className="pp";
	div.innerHTML="请输入段落";
	editable(div);
	frag.appendChild(div);
	//创建控制span
	var span=document.createElement("span");
	span.className="control";
	var a=document.createElement("a");
	a.href="javascript:void(0)";
	a.innerHTML="在之后添加段落";
	a.setAttribute("onclick","addP(this)");;
	span.appendChild(a);
	
	a=document.createElement("a");
	a.href="javascript:void(0)";
	a.innerHTML="删除";
	a.setAttribute("onclick","delP(this)");;
	span.appendChild(a);
	
	frag.appendChild(span);
	parent.parentNode.insertBefore(frag,next);
}

function delP(elem){
	var parent=elem.parentNode;
	var prev=parent.previousSibling;
	parent.parentNode.removeChild(prev);
	parent.parentNode.removeChild(parent);
}

//style方式
function style_choose(elem){
	var style=document.getElementById("style");
	var style_child=style.childNodes;
	var value=elem.value;
	for(var i=0,len=style_child.length;i<len;i++){
		if(style_child[i].nodeName=="DIV"){
			if(style_child[i].className!=""){
				style_child[i].style.display="none";
			}
			if(style_child[i].className=="style_"+value){
				style_child[i].style.display="block";
			}
		}
	}
}
//是否添加音乐
function music_choose(elem){
	if(elem.value=="music_yes"){
		document.getElementById("music_choose").style.display="block";
	}else if(elem.value=="music_no"){
		document.getElementById("music_choose").style.display="none";
	}
}
//提交信息
function form_submit(){
	var form=document.forms["love_set"];
	form.onsubmit=function(event){
		preventDefault(event);
		var info=new Object();
		get_info(info);
		var str=JSON.stringify(info);
		//得到设置
		var set=new Object();
		var set_str="";
		if(get_set(set)){
			set_str=JSON.stringify(set);
			$.get("../back_stage/edit_stage.php",{newEdit:true,info:str,set:set_str},function(text){
				if(text=="ok"){
					window.open("user_center.php","_self");
				}
			},false);
		}
	}
	document.getElementById("yulan").onclick=function(){
		var info=new Object();
		get_info(info);
		var str=encodeURI(JSON.stringify(info));
		//得到设置
		var set=new Object();
		var set_str="";
		if(get_set(set)){
			set_str=encodeURI(JSON.stringify(set));
			$.get("../back_stage/edit_stage.php",{yulanEdit:true,info:JSON.stringify(info),set:JSON.stringify(set)},function(text){
				if(text=="ok"){
					window.open("tmp.php","_blank");
				}
			},false);
			
		}
	}
}
//获取内容信息
function get_info(elem){
	//得到标题，昵称
	elem.header=str_trim(document.getElementById("header").innerHTML);
	elem.tishi=str_trim(document.getElementById("tishi").innerHTML);
	elem.start=str_trim(document.getElementById("header").innerHTML);
	//得到内容
	elem.pp=new Array();
	var start=document.getElementById("start");
	while(start=start.nextSibling){
		if(start.nodeName=="DIV"){
			if(start.id=="final"){
				break;
			}else if(start.className="pp"){
				var str=str_trim(start.innerHTML);
				elem.pp.push(str);
			}
		}
	}
	elem.final=str_trim(document.getElementById("final").innerHTML);
}
//获取设置信息
function get_set(elem){
	var go=true;
	var form=document.forms["love_set"];
	//背景
	var bg_style=form.elements["style"][0];
	if(bg_style.checked){
		elem.background="#"+form.elements["style_color_value"].value;
	}else{
		var image_url=document.getElementById("o_iframe1").contentWindow.document.body.firstChild.nodeValue;
		image_url=str_trim(image_url);
		if(image_url!=""){
			//位置
			image_url="url('"+image_url+"')";
			var position_hor=radio_value(form.elements["position_hor"]);
			var position_ver=radio_value(form.elements["position_ver"]);
			var repeat=radio_value(form.elements["repeat"]);
			var arr=[image_url,position_hor,position_ver,repeat];
			elem.background=arr.join(" ");
			go=true;
		}else{
			alert("请先上传文件");
			go=false;
		}
	}
	//音乐
	var music_style=form.elements["music_have"][0];
	if(music_style.checked){
		var music_url=document.getElementById("o_iframe2").contentWindow.document.body.firstChild.nodeValue;
		music_url=str_trim(music_url);
		if(music_url!=""){
			//位置
			var music_repeat=radio_value(form.elements["music"]);
			elem.music=music_url;
			elem.musicrepeat=music_repeat;
			go=true;
		}else{
			alert("请先上传文件");
			go=false;
		}
	}
	return go;
}
//取得选中单选的值
function radio_value(nodelist){
	for(var i=0,len=nodelist.length;i<len;i++){
		if(nodelist[i].checked){
			return nodelist[i].value;
		}
	}
}
window.onload=function(){
	var main=document.getElementById("main")
	var divs=main.childNodes;
	for(var i=0,len=divs.length;i<len;i++){
		if(divs[i].nodeName=="DIV"){
			editable(divs[i]);
		}
	}
	form_submit();
}